package com.mosai.smartmedical;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.blankj.utilcode.util.AppUtils;
import com.bumptech.glide.Glide;
import com.mosai.alignpro.CameraActivity;
import com.mosai.alignpro.DiagnosisActivity;
import com.mosai.alignpro.util.IntentUtils;
import com.mosai.alignpro.util.LanguageUtils;
import com.mosai.alignpro.util.LogUtils;
import com.mosai.smartmedical.util.DataUtils;
import com.yalantis.ucrop.UCrop;

import java.io.File;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();
    public static final int REQUEST_CODE_CAMERA = 100;
    public static final int REQUEST_CODE_DIAGNOSIS_LIST = 101;
    public static final int REQUEST_CODE_DIAGNOSIS = 102;

    private ImageView ivAvatar;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LanguageUtils.attachBaseContext(newBase));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
        setContentView(R.layout.activity_main);

        ivAvatar = findViewById(R.id.ivAvatar);
        Glide.with(this)
                .load("http://a0.att.hudong.com/16/12/01300535031999137270128786964.jpg")
                .into(ivAvatar);

//        onNotificationListener(null);
//        onDiagnosisListByWaiting(null);
        onDiagnosisListByWaitingNoPatient(null);
//        onQRCode(null);
//        onSerial(null);
//        onSensor(null);
//        onDiagnosisListByAudited(null);
//        onDiagnosisListByPatientApp(null);
//        onPageThree(null);
//        onPageOne(null);
//        onCamera(null);
//        onCrop(null);

        Log.i(TAG, "onCreate: google play is install: " + AppUtils.isAppInstalled("com.android.vending"));
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "onNewIntent: ");
    }

    public void onCamera(View v) {
//        String param = null;
        //language=JSON
        String param = "{\"imageType\": 5}";
        CameraActivity.startForResult(this, REQUEST_CODE_CAMERA, param);
    }

    public void onCrop(View v) {
        // content://media/external/images/media/188
        Uri destination = Uri.fromFile(new File(getExternalCacheDir(), System.currentTimeMillis() + ".jpg"));
        Uri source = Uri.parse("content://media/external/images/media/188");
        UCrop.Options options = new UCrop.Options();
        options.useSourceImageAspectRatio();
//        options.setToolbarColor(Color.BLACK);
//        options.setToolbarWidgetColor(Color.WHITE);
//        options.setStatusBarColor(Color.GREEN);
//        options.setRootViewBackgroundColor(Color.parseColor("#f0f0f0"));
//        options.setActiveControlsWidgetColor(Color.BLUE);
        UCrop.of(source, destination)
//                .withAspectRatio(16, 9)
                .withMaxResultSize(720, 720)
                .withOptions(options)
                .start(this);
    }

    public void onShowResult(View v) {
        DiagnosisActivity.startForResult(this, REQUEST_CODE_DIAGNOSIS);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) {
            assert data != null;
            String uri = data.getStringExtra(CameraActivity.EXTRA_URI);
            int imageType = data.getIntExtra(CameraActivity.EXTRA_IMAGE_TYPE, 1);
            LogUtils.d(TAG, "imageType: " + imageType + " uri: " + uri);
            Glide.with(this)
                    .load(uri)
                    .into(ivAvatar);

        }
    }

    public void onPageOne(View view) {
        startActivity(new Intent(this, PageOneActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    public void onDiagnosisListByAudited(View view) {
        IntentUtils.setDoctorApp();
        String data = DataUtils.getRnParamByAudited(this);
        IntentUtils.goDiagnosisList(this, REQUEST_CODE_DIAGNOSIS_LIST, data);
    }

    public void onDiagnosisListByWaitingNoPatient(View view) {
        IntentUtils.setDoctorApp();
        String data = DataUtils.getRnParamByWaitingNoPatient(this);
        IntentUtils.goDiagnosisList(this, REQUEST_CODE_DIAGNOSIS_LIST, data);
    }

    public void onDiagnosisListByWaiting(View view) {
        IntentUtils.setDoctorApp();
        String data = DataUtils.getRnParamByWaiting(this);
        IntentUtils.goDiagnosisList(this, REQUEST_CODE_DIAGNOSIS_LIST, data);
    }

    public void onDiagnosisListByPatientDetail(View view) {
        IntentUtils.setDoctorApp();
        String data = DataUtils.getRnParamByPatientDetail(this);
        IntentUtils.goDiagnosisList(this, REQUEST_CODE_DIAGNOSIS_LIST, data);
    }

    public void onDiagnosisListByPatientApp(View view) {
        IntentUtils.setPatientApp();
        String data = DataUtils.getRnParamByPatientApp(this);
        IntentUtils.goDiagnosisList(this, REQUEST_CODE_DIAGNOSIS_LIST, data);
    }

    public void onPageThree(View view) {
        startActivity(new Intent(this, PageThreeActivity.class));
    }

    public void onLanguageZH(View view) {
        LanguageUtils.setLanguage("zh");
    }

    public void onLanguageEN(View view) {
        LanguageUtils.setLanguage("en");
    }

    public void onSensor(View view) {
        startActivity(new Intent(this, SensorActivity.class));
    }

    public void onSerial(View view) {
        startActivity(new Intent(this, SerialActivity.class));
    }

    public void onQRCode(View view) {
        startActivity(new Intent(this, QRCodeActivity.class));
    }

    public void onNotificationListener(View view) {
        startActivity(new Intent(this, NotificationListenerActivity.class));
    }

    public void onSmsListener(View view) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 100);
        }
    }

    public void onPermission(View view) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(this, "已授权", Toast.LENGTH_SHORT).show();
            return;
        }
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Log.i(TAG, "onRequestPermissionsResult:"
                + " \nrequestCode: " + requestCode
                + " \npermissions: " + Arrays.toString(permissions)
                + " \ngrantResults: " + Arrays.toString(grantResults)
        );
    }
}
